# import time
# from threading import Lock
# from urllib.parse import urlparse
#
# import pytest
# from py._xmlgen import html
#
#
# # # 修改html中的Environment部分
# # def pytest_configure(config):
# #     config._metadata['测试环境'] = config_.host_zxyj
#
#
# # 修改html中的Summary部分
# @pytest.mark.optionalhook
# def pytest_html_results_summary(prefix, summary, postfix):
#     prefix.extend([html.p('测试人: 听风')])
#     # summary.extend(html.p('【摘要: 666】' ))
#     # postfix.extend(html.p('【后缀】'))
#
#
# # 设置列名
# @pytest.mark.optionalhook
# def pytest_html_results_table_header(cells: list):
#     cells.clear()  # 清除所有英文列表
#     cells.append(html.th('测试结果', class_="sortable result initial-sort", col="result"))
#     cells.append(html.th('用例id'))
#     cells.append(html.th('用例标题'))  # doc属性, 如果要排序, 估计要用的 "class_='sortable ...'"
#     cells.append(html.th("url"))
#     cells.append(html.th('持续时间', class_="sortable numeric", col="duration"))
#     cells.append(html.th('开始时间', class_='sortable time', col='time'))
#
#
# # 设置每列的内容
# @pytest.mark.optionalhook
# def pytest_html_results_table_row(report, cells: list):
#     """
#     # 默认有4列, 分别是：
#     # 测试结果、用例节点、持续时间、links
#     # 1. 把用例节点, 换成doc属性
#     # 2. 然后再删除最后的links节点, 并补上其他列内容
#     """
#     # cells.insert(0, "测试结果")  # 模拟测试结果
#     id_, doc, url = (getattr(report, x, "...") for x in ["id", "doc", "url"])
#     cells[1] = html.td(id_, class_="case_id")
#     cells.insert(2, html.td(doc, class_="case_name"))
#     cells.insert(3, html.td(url, class_="url"))
#     # cells.insert(4, "持续时间")  # 模拟持续时间
#     cells.insert(5, html.td(time.strftime("%H:%M:%S"), class_='col-time'))
#     cells.pop()  # 删除最后一个"links"
#
#
# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item):
#     """
#     添加用例的注释、开始时间、设置utf-8编码
#     注意：虽然使用的是函数实例化之后的doc,
#     但也需要先创建默认属性。
#     """
#     # 先设定function的属性, 再设置report的属性
#     outcome = yield
#     report = outcome.get_result()
#     func = item.function
#     # pytest_html = item.config.pluginmanager.getplugin('html')
#     # 自定义的用例描述格式
#     # report.doc = description_html(item.function.__doc__)
#     extra = getattr(report, 'extra', [])
#     if report.when == 'call' or report.when == "setup":
#         report.extra = extra
#         doc = func.__doc__ or "..."
#         report.doc = doc.strip()
#         report.id = getattr(func, "id", 0)
#         url = str(getattr(func, "url", "...")).strip()
#         url = urlparse(url).path
#         report.url = url or ""
#         # 设置编码显示中文
#         report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
#
#
# def description_html(desc):
#     """
#     将用例中的描述转成HTML对象
#     :param desc: 描述
#     :return:
#     """
#     if desc is None:
#         desc = ' 【描述...】'
#         # return "No case description"
#     desc_ = ""
#     for i in range(len(desc)):
#         if i == 0:
#             pass
#         elif desc[i] == '\n':
#             desc_ = desc_ + ";"
#         else:
#             desc_ = desc_ + desc[i]
#
#     desc_lines = desc_.split(";")
#     desc_html = html.html(
#         html.head(
#             html.meta(name="Content-Type", value="text/html; charset=latin1")),
#         html.body(
#             [html.p(line) for line in desc_lines]))
#     return desc_html
#
#
# """主要用于定义html报告"""
